from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())

from openai import OpenAI
client = OpenAI()
model = "tts-1"

from pathlib import Path
cur_path = Path(__file__).parent

while True:  
    text = input("请输入待转换文本（输入'exit'退出程序）: ") 
    if text is None or text == "" :
        continue
    elif text.lower() == 'exit':  
        print("程序退出。")  
        break 
    else:
        response = client.audio.speech.create(
            model=model,
            voice="alloy",
            input=text
        )

        from datetime import datetime 
        now = datetime.now()
        numeric_date_string = now.strftime("%Y%m%d%H%M%S")
        speech_file_path = f"{cur_path}/{numeric_date_string}.mp3"
        response.write_to_file(speech_file_path)
        response.close()